package com.os.opencv.java.chapter3;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class DetectColor {

    public static void main(String[] args) {
        ///Users/matt/MATT-MAC/matt_workspace/OS-OpenCV_workspace/eyeTest.jpeg
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        //将图像读取并在屏幕显示
        Mat src = Imgcodecs.imread("/Users/matt/MATT-MAC/matt_workspace/OS-OpenCV_workspace/eyeTest.jpeg");
        HighGui.imshow("blind", src);
        HighGui.waitKey(0);

        //将彩色图像转换为灰度图像并在屏幕显示
        Mat grey = new Mat();
        Imgproc.cvtColor(src, grey, Imgproc.COLOR_BGR2GRAY);
        HighGui.imshow("gray", grey);
        HighGui.waitKey(0);

        //将图像转换为HSV颜色模型
        Mat hsv = new Mat();
        Imgproc.cvtColor(src, hsv, Imgproc.COLOR_BGR2HSV);
        HighGui.imshow("hsv", hsv);
        HighGui.waitKey(0);

        Mat dst = src.clone();

        //搜索政府图像并根据色调值分割图像
        for(int i=0; i<hsv.rows(); i++){
            for(int j=0; j<hsv.cols(); j++){
                //获取hsv颜色模型中的色调值H值
                byte[] data = new byte[3];
                hsv.get(i, j, data);

                //红绿色盲，只要区分出红色和绿色即可
                //根据色调值判断，如为绿色系（45 ~ 80）则用黑色画出，否则用白色画出
                if((data[0] > 45) & (data[0] < 80)){
                    data[0] = 0;
                    data[1] = 0;
                    data[2] = 0;
                }else{
                    //byte类型的-1将被映射为CV_8U类型的255
                    data[0] = -1;
                    data[1] = -1;
                    data[2] = -1;
                }
                //修改dst中RGB的值
                dst.put(i, j, data);
            }
        }

        //在屏幕上显示用颜色分割后的图像
        HighGui.imshow("divided", dst);
        HighGui.waitKey(0);
        System.exit(0);
    }
}
